<template>
  <div class="marketing-seckill-container">
    <!-- 操作 -->
    <el-row class="marketing-seckill-row">
      <el-col :span="14" style="text-align:left;padding-left:15px" class="marketing-seckill-row-col">
        <el-button type="primary" size="small" style="padding:8px 30px;" @click="add">新 增</el-button>
      </el-col>
      <el-col :span="10">
        <el-input v-model="searchName" placeholder="请输入名称" size="small" style="max-width:350px">
          <el-select v-model="state" slot="prepend" placeholder="请选择" size="small" style="min-width:100px">
            <el-option label="未选择" value=""></el-option>
          </el-select>
          <el-button slot="append" icon="el-icon-search" size="small" @click="search">
          </el-button>
        </el-input>
        
      </el-col>
    </el-row>

    <!-- 表格 -->
    <div>
      <el-table :data="seckillList" style="width:100%" stripe>
        <el-table-column prop="id" label="编号" align="center"></el-table-column>
        <el-table-column prop="name" label="活动名称" align="center"></el-table-column>
        <el-table-column prop="detail_info" label="描述" align="center"></el-table-column>
        <el-table-column prop="format_create_time" label="创建时间" align="center"></el-table-column>
        <el-table-column prop="start_time" label="开始时间" align="center"></el-table-column>
        <el-table-column prop="end_time" label="结束时间" align="center"></el-table-column>
        <el-table-column label="状态" align="center">
          <template slot-scope="scope">
            <div>
              {{ scope.row.state | State }}
            </div>
          </template>
        </el-table-column>
        <el-table-column label="操作" align="center">
          <template slot-scope="scope">
            <el-button class="action-button" type="text" 
              @click="eidt(scope.row)"><i class="el-icon-edit"/></el-button>
            <el-button class="action-button" 
              @click="del(scope.row)" type="text">
              <svg-icon icon-class="停止"/>
            </el-button>
          </template>
        </el-table-column>
      </el-table>
    </div>

    <!-- 分页 -->
    <div style="text-align:right;margin:15px 15px">
      <el-pagination class="page"
        background
        layout="total, prev, pager, next, jumper"
        :total="count"
        :page-size="10"
        :current-page="page"
        @size-change="changePage"
        @current-change="changePage">
      </el-pagination>
    </div>

    <!-- 新增 编辑 -->
    <el-dialog :visible.sync="disAdd" width="60%" :title="isAdd ? '新增' : '修改'" top="0">
      <add :isAdd="isAdd" :skillData="skillData" @success="disAdd = false; getSeckillList()" @addGoodList='addGoodList' v-if="disAdd"/>
    </el-dialog>
  </div>
</template>

<script>
import { getSeckillList, getSeckillSwitch, putSeckillSwitch, delSeckill } from '@/api/marketing'

import add from './components/add'

export default {
  name: 'seckill',
  components: {
    add
  },
  data() {
    return {
      page: 1,
      count: 1,

      skillSwitch: { id: '',is_open: '1' },
      searchName: '',
      state: '',

      seckillList: [],

      isAdd: true,
      disAdd: false,

      skillData: {},
    }
  },
  mounted() {
    this.getSeckillList();
    this.getSeckillSwitch();
  },
  filters: {
    State: (e) => {
      let json = {
        1: '未开始', 
        2: '进行中',
        3: '已结束',
        4: '已关闭',
      }
      return json[e]
    }
  },
  methods: {
    /**
     * 获取秒杀列表
     */
    getSeckillList() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: this.page,
        limit: 10
      };

      getSeckillList(params).then(res => {
        if(res.status === 200) {
          this.seckillList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204) {
          this.seckillList = [],
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 获取恋爱总开关
     */
    getSeckillSwitch() {
      getSeckillSwitch({key: this.$store.state.app.activeApp.saa_key}).then(res => {
        if(res.status === 200) {
          this.skillSwitch = res.data;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 修改秒杀总开关
     */
    putSeckillSwitch() {
      putSeckillSwitch({key: this.$store.state.app.activeApp.saa_key, ...this.skillSwitch}).then(res => {
        if(res.status === 200) {
          this.$message.success('修改成功');
        }else{
          this.$message.error(res.message);
        }
      })
    },
    /**
     * 新增
     */
    add() {
      this.skillData = {
        key: this.$store.state.app.activeApp.saa_key,
        name: '',
        detail_info: '',
        start_time: '',
        end_time: '',
        send_time: '',
        goods_list: []
      }
      this.disAdd = true;
      this.isAdd = true;
    },
    /**
     * 编辑
     */
    eidt (e) {
      this.skillData = {
        id: e.id,
        key: this.$store.state.app.activeApp.saa_key,
        name: e.name,
        detail_info: e.detail_info,
        start_time: e.start_time,
        end_time: e.end_time,
        send_time: e.send_time,
        goods_list: e.goods_list.map(e => {
          return {
            id: e.goods_id, 
            flash_number: e.property.map(e => e.stocks),
            flash_original_price: e.property.map(e => e.flash_original_price),
            flash_name: e.property.map(e => e.name) || [e.name],
            flash_price: e.property.map(e => e.flash_price),
            flash_id: e.property.map(e => e.stock_id),
            property1_name: e.property.map(e => e.property1_name),
            property2_name: e.property.map(e => e.property2_name),
            pic_url: e.pic_urls,
            is_top: 0,
            name: e.name,
            line_price: e.line_price
          }
        })
      }
      this.disAdd = true;
      this.isAdd = false;
    },
    del(e){
      this.$confirm('是否删除该条记录?','提示',{
        confirmButtonText: '是',
        cancelButtonText: '否',
        type: 'warning'
      }).then(() => {
        delSeckill(e).then(res => {
          console.log(res)
          this.$message.success('删除成功');
          this.getSeckillList();
        })
      }).catch(() => {

      })
    },
    /**
     * 翻页
     */
    changePage(page) {
      this.page = page;
      this.getSeckillList();
    },
    /**
     * 搜索
     */
    search() {
      const params = {
        key: this.$store.state.app.activeApp.saa_key,
        page: 1,
        limit: 10,
        searchName: this.searchName
      };

      getSeckillList(params).then(res => {
        if(res.status === 200) {
          this.seckillList = res.data;
          this.count = parseInt(res.count);
        }else if(res.status === 204) {
          this.seckillList = [],
          this.count = 1;
        }else{
          this.$message.error(res.message);
        }
      })
    },
    addGoodList(e) {
      this.skillData.goods_list.push(e.goods)
    }
  },
}
</script>

<style lang="scss" scoped>
.marketing-seckill-container{
  min-height: calc(100vh - 100px);
  margin: 20px;
  background-color: #fff;
  padding:25px;
  box-shadow: 0px 0px 4px 0px rgba(115, 115, 115, 0.2);
}
.marketing-seckill-row{
  margin: 15px 0;
  text-align: center;
  height: 40px;
  line-height: 40px;
  &-col{
    padding: 0 15px;
    max-width: 95%;
    overflow: hidden;
    display: flex;
  }
}
</style>